package com.zzyl.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.properties.WechatConfigProperties;
import com.zzyl.service.WechatService;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WechatServiceImpl implements WechatService {

    @Autowired
    private WechatConfigProperties wechatConfigProp;

    //调用微信接口 获取openId的url
    final private String GET_OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    //调用微信接口 获取access_token的url
    final private String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
    //调用微信接口 获取手机号的url
    final private String POST_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    @Override
    public String getOpenId(String code) {
        Map<String, Object> paramMap = getParamMap();
        paramMap.put("js_code",code);
        paramMap.put("grant_type","authorization_code");
        String jsonStr = HttpUtil.get(GET_OPEN_ID_URL,paramMap);
        log.info("获取的openId的jsonStr:{}",jsonStr);
        //把json字符串转换成对象
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        //判断json对象是否为空
        if (jsonObject == null){
            log.error("获取openId失败");
            throw new BaseException(BasicEnum.GET_OPENID_ERROR);
        }
        //获取openId
        String openId = jsonObject.getStr("openid");
        return openId;
    }

    @Override
    public String getPhone(String phoneCode) {
        //获取access_token
        String accessToken = getAccessToken();
        //获取手机号
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code",phoneCode);
        String phoneStr = HttpUtil.post(POST_PHONE_URL + accessToken,JSONUtil.toJsonStr(paramMap));
        //解析获取手机号
        JSONObject phoneJsonObject = JSONUtil.parseObj(phoneStr);
        JSONObject phoneInfo = phoneJsonObject.getJSONObject("phone_info");
        if (phoneInfo == null){
            log.info("获取手机号失败");
            throw new BaseException(BasicEnum.GET_PHONE_ERROR);
        }
        String phoneNumber = phoneInfo.getStr("phoneNumber");
        log.info("获取的手机号:{}",phoneNumber);
        return phoneNumber;
    }

    //获取paramMap 包括appId appSecret
    private Map<String, Object> getParamMap() {
        //创建一个集合来装请求参数
        HashMap<String, Object> paramMap = new HashMap<>();
        //放入appId appSecret
        paramMap.put("appid",wechatConfigProp.getAppid());
        paramMap.put("secret",wechatConfigProp.getSecret());
        return paramMap;
    }

    //获取accessToken
    private String getAccessToken(){
        //获取paramMap
        Map<String, Object> paramMap = getParamMap();
        //将grant_type参数设置到集合中
        paramMap.put("grant_type","client_credential");
        //请求获取access_token
        String jsonStr = HttpUtil.get(GET_ACCESS_TOKEN_URL,paramMap);
        //获取access_token
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        //判断一下是否为空
        if (jsonObject == null){
            log.error("获取access_token失败");
            throw new BaseException(BasicEnum.GET_TOKEN_ERROR);
        }
        String accessToken = jsonObject.getStr("access_token");
        return accessToken;
    }



}
